// pages/orderDetail/orderDetail.js
var that,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isChecked: !0,
    isChecked2: !0,
    showmodel: !1,
    showconfirm: !1,
    showbalance: !1,
    iserr: !1,
    isFocus: false,//控制input 聚焦
    isshow: !1,
    addressList: [],
    selindex: 0,
    addressid: 0,
    addressinfo: "",
    num: 1,
    useScore: 1,
    useAccount: 1,
    isenough: !1,
    isget: !1,
    seltype: 0,
    issucc: !1,
    brief:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    let { detail } = options, orderdata = ''
    orderdata = JSON.parse(detail)
    that.setData({ orderdata })
  },
  seltype(e) {
    let { index } = e.currentTarget.dataset
    that.setData({ seltype: index },()=>{
      that.checkbudget()
    })
  },
  getdata() {
    let { addressList, addressinfo, isChecked, isChecked2, useScore, useAccount, orderdata, num } = that.data, checkbuddata = '',  {  msgTplIdArr } = app.logininfo
    that.setData({msgTplIdArr})

    if(orderdata.storeType!=2){
      api.getaddressList(res => {
        if (res.data.code == 0) {
          if(orderdata.storeType==1){
            console.log(33333);
            if (res.data.entity) {
              addressList = res.data.entity.data
                if (addressList.length > 0) {
                  addressinfo = addressList[0]
                  that.setData({ addressList, addressinfo }, () => {
                    that.checkbudget()
                  })
                }else {
                  wx.showModal({
                    title: '提示',
                    content: '请先添加地址',
                    success(res) {
                      if (res.confirm) {
                        wx.navigateTo({
                          url: '../addAderss/addAderss',
                        })
                      }
                      if (res.cancel) {
                        wx.navigateBack()
                      }
                    }
                  })
                }
            }else{
              wx.showModal({
                title: '提示',
                content: '请先添加地址',
                success(res) {
                  if (res.confirm) {
                    wx.navigateTo({
                      url: '../addAderss/addAderss',
                    })
                  }
                  if (res.cancel) {
                    wx.navigateBack()
                  }
                }
              })
            }
          }else{
            if (res.data.entity) {
              addressList = res.data.entity.data
                if (addressList.length > 0) {
                  addressinfo = addressList[0]
                  that.setData({ addressList, addressinfo }, () => {
                    that.checkbudget()
                  })
                }
            }else{
              that.checkbudget()
            }
          }
        }
      })
    }else{
      that.setData({seltype:1},()=>{
        that.checkbudget()
      })
    }
  },
  checkbudget() {
    let { useAccount, useScore, addressinfo, orderdata, num, isChecked2, isChecked, seltype } = that.data, checkbuddata = '', aid = ''
    if(seltype==0){
      if(addressinfo==''){
        aid=0
      }else{
        aid=addressinfo.id
      }
    }else{
      aid=0
    }
    // aid = seltype == 0 ? addressinfo.id : 0
    api.checkbudget({ useAccount, useScore, aid, id: orderdata.id, num }, res => {
      if (res.data.code == 0) {
        checkbuddata = res.data.entity.data
        if (checkbuddata.account == 0 && isChecked2) {
          checkbuddata.useAccount = 0
          if (checkbuddata.banScoreUse == 1) {
            checkbuddata.useScore = 0
            checkbuddata.score = 0
            that.setData({isChecked:!1,useScore:0})
          }
          that.setData({ showconfirm: !0, isChecked2: false, checkbuddata })
        } else {
          if (checkbuddata.banScoreUse == 1) {
            checkbuddata.useScore = 0
            checkbuddata.score = 0
            that.setData({isChecked:!1,useScore:0})
          }
          that.setData({ checkbuddata })
        }
      }
    })
  },
  checkbudget2() {
    let { useAccount, useScore, addressinfo, orderdata, num, isChecked2, isChecked ,seltype} = that.data, checkbuddata = '', aid = ''
    if(seltype==0){
      if(addressinfo==''){
        aid=0
      }else{
        aid=addressinfo.id
      }
    }else{
      aid=0
    }
    // aid = seltype == 0 ? addressinfo.id : 0
    api.checkbudget({ useAccount, useScore, aid, id: orderdata.id, num }, res => {
      if (res.data.code == 0) {
        checkbuddata = res.data.entity.data
        if (checkbuddata.banScoreUse == 1&&isChecked) {
          console.log(checkbuddata.scoreSwitch,'orderdata.scoreSwitch');
          if(checkbuddata.scoreSwitch==0){
            console.log(1111111);
            wx.showModal({
              title: '提示',
              content:'该站暂不支持积分兑换服务哦!',
              showCancel: !1,
              success(res) {
                if (res.confirm) {
                  checkbuddata.useScore = 0
                  checkbuddata.score = 0
                  that.setData({ isChecked: false, checkbuddata, useScore: 0 })
                }
              }
            })
          }else{
            console.log(22222);
            wx.showModal({
              title: '提示',
              content:orderdata.fixedScore>0?'您的积分不足，暂不能使用积分抵扣':'该订单不可使用积分抵扣',
              showCancel: !1,
              success(res) {
                if (res.confirm) {
                  checkbuddata.useScore = 0
                  checkbuddata.score = 0
                  that.setData({ isChecked: false, checkbuddata, useScore: 0 })
                }
              }
            })
          }
        } else {
          that.setData({ checkbuddata })
        }
      }
    })
  },
  bindchange1(e) {
    let { useScore } = that.data
    if (e.detail.value) {
      useScore = 1
    } else {
      useScore = 0
    }
    that.setData({ isChecked: e.detail.value, useScore }, () => {
      that.checkbudget2()
    })
  },
  bindchange2(e) {
    let { useAccount } = that.data
    if (e.detail.value) {
      useAccount = 1
    } else {
      useAccount = 0
    }
    that.setData({ isChecked2: e.detail.value, useAccount }, () => {
      that.checkbudget()
    })
  },
  toseladdress(e) {
    let { index, id } = e.currentTarget.dataset, { selindex, addressid, addressinfo, addressList } = that.data
    if (index == selindex) return
    addressinfo = addressList[index]
    console.log(addressinfo, 'addressinfoaddressinfoaddressinfo');
    that.setData({ selindex: index, addressid: id, addressinfo })
  },
  close() {
    that.setData({ showmodel: !1 })
  },
  showAdressModel() {
    that.setData({ showmodel: !0 })
  },
  closeconfirm() {
    that.setData({ showconfirm: !1 })
  },
  closebalance() {
    that.setData({ isenough: !1 })
  },
  showbalance() {
    that.setData({ isenough: !0 })
  },
  addAderss(e) {
    let { id } = e.currentTarget.dataset
    if (id) {
      wx.navigateTo({
        url: '../addAderss/addAderss?id=' + id,
      })
    } else {
      wx.navigateTo({
        url: '../addAderss/addAderss',
      })
    }
  },
  set_wallets_password(e) {//获取钱包密码，余额支付
    // console.log(e,'eeeeeeeeeeeee');
          
    let { orderId,seltype } = that.data, neworderId = orderId * 1
    this.setData({
      wallets_password: e.detail.value
    });
    if (this.data.wallets_password.length == 6) {//密码长度6位时，自动验证钱包支付结果
      let pwd = e.detail.value
      api.bulkaccountpay({ orderId: neworderId, pwd }, res => {
        if (res.data.code == 0) {
          that.setData({ isFocus: !1,isshow: !1,}, () => {
            if(seltype==0){
              wx.showModal({
                title: '提示',
                content: '支付成功',
                success(res) {
                  wx.redirectTo({
                    url: '../orderList/orderList',
                  })
                }
              })
            }else{
              that.setData({issucc:!0})
            }
            // wx.navigateTo({
            //   url: '../orderpaysucc/orderpaysucc?orderId=' + orderId,
            // })
          })
        } else if (res.data.code == 20312) {
          that.setData({ iserr: !0, isshow: !1, isFocus: !1, iscon: !1 })
        } else {
          that.setData({ isenough: !0, isshow: !1, isFocus: !1, iscon: !1 })
        }
      })
    }
  },
  close_wallets_password() {
    that.setData({ iscon: !1, isshow: !1 })
  },
  torecharge() {
    wx.navigateTo({
      url: '../recharge/recharge',
    })
  },
  closebu() {
    that.setData({
      isenough: !1,
      isshow: !0,
      wallets_password: ''
    })
  },
  retry() {
    that.setData({
      iserr: !1,
      iscon: !0,
      wallets_password: '',
      isFocus: !0,
      wallets_password: ''
    })
  },
  modify_password() {
    that.setData({
      iserr: !1,
      iscon: !1
    }, () => {
      wx.navigateTo({
        url: '../forgetPwd/forgetPwd',
      })
    })
  },
  set_Focus() {//聚焦input
    console.log('isFocus', this.data.isFocus)
    this.setData({
      isFocus: true
    })
  },
  addnum(e) {
    let { type } = e.currentTarget.dataset, { num } = that.data
    if (type == 'reduce') {
      num = num + 1
    } else {
      if (num > 1) {
        num = num - 1
      } else {
        wx.showToast({
          title: '不能再减了',
          icon: 'none'
        })
        return
      }
    }
    that.setData({ num }, () => {
      that.checkbudget()
    })
  },
  inputtap(e) {
    that.setData({ brief: e.detail.value })
  },
  pay() {
    let { checkbuddata, num, addressinfo, orderdata, brief, seltype } = that.data, aid = ''
    if(seltype==0&&addressinfo==''){
      wx.showModal({
        title: '提示',
        content:'请先添加收货地址',
        success(res){
          if(res.confirm){
              wx.navigateTo({
                url: '../addAderss/addAderss',
              })
          }
        }
      })
      return
    }
    let  {msgTplIdArr} = that.data,newmsgTplIdArr=[]
    newmsgTplIdArr.push(msgTplIdArr.orderPlan,msgTplIdArr.refundAlert)
    wx.requestSubscribeMessage({
      tmplIds: newmsgTplIdArr,
      success: function (res) {
        console.log(res, 'eeeeeeee')
        wx.showLoading({
          title: '加载中',
        })
        aid = seltype == 0 ? addressinfo.id : 0
        api.placeorder({ id: orderdata.id, aid, num, useAccount: checkbuddata.useAccount, useScore: checkbuddata.useScore, brief }, res1 => {
          if (res1.data.code == 0) {
            let { orderId, payType } = res1.data.entity.data
            if (payType == 1) {
              wx.hideLoading()
              that.setData({ isshow: !0, orderId })
            } else {
              api.bulkwxstartpay({ orderId }, res => {
                wx.hideLoading()
                console.log(res, 'res');
                if (res.succ == 1) {
                  api.bulkgrantcoupon(orderId, res => {
                    if (res.data.code == 0) {
                      if (res.data.entity.data.num > 0) {
                        let { couponArr } = res.data.entity.data
                        that.setData({ isget: !0, couponArr })
                      } else {
                        if(seltype==0){
                          wx.showModal({
                            content: '支付成功',
                            showCancel: !1,
                            success(res) {
                              if (res.confirm) {
                                wx.redirectTo({
                                  url: '../orderList/orderList',
                                })
                              }
                            }
                          })
                        }else{
                          that.setData({issucc:!0})
                        }
                      }
                    }
                  })
                } else {
                  wx.showModal({
                    content: res.msg,
                    showCancel: !1,
                  })
                }
              })
            }
          }else{
            wx.hideLoading()
            console.log(res1.data.message,'res1.data.message');
            wx.showModal({
              title:'提示',
              content: res1.data.message,
              showCancel:!1
            })
          }
        })
      },
      fail: function (err) {
        console.log(err, 'rrrrrrrrr')
        wx.showLoading({
          title: '加载中',
        })
        aid = seltype == 0 ? addressinfo.id : 0
        api.placeorder({ id: orderdata.id, aid, num, useAccount: checkbuddata.useAccount, useScore: checkbuddata.useScore, brief }, res1 => {
          if (res1.data.code == 0) {
            let { orderId, payType } = res1.data.entity.data
            if (payType == 1) {
              wx.hideLoading()
              that.setData({ isshow: !0, orderId })
            } else {
              api.bulkwxstartpay({ orderId }, res => {
                wx.hideLoading()
                console.log(res, 'res');
                if (res.succ == 1) {
                  api.bulkgrantcoupon(orderId, res => {
                    if (res.data.code == 0) {
                      if (res.data.entity.data.num > 0) {
                        let { couponArr } = res.data.entity.data
                        that.setData({ isget: !0, couponArr })
                      } else {
                        if(seltype==0){
                          wx.showModal({
                            content: '支付成功',
                            showCancel: !1,
                            success(res) {
                              if (res.confirm) {
                                wx.redirectTo({
                                  url: '../orderList/orderList',
                                })
                              }
                            }
                          })
                        }else{
                          that.setData({issucc:!0})
                        }
                      }
                    }
                  })
                } else {
                  wx.showModal({
                    content: res.msg,
                    showCancel: !1,
                  })
                }
              })
            }
          }else{
            wx.hideLoading()
            console.log(res1.data.message,'res1.data.message');
            wx.showModal({
              title:'提示',
              content: res1.data.message,
              showCancel:!1
            })
          }
        })
      }
    })
   
  },
  toorder(){
    wx.redirectTo({
      url: '../orderList/orderList',
    })
  },
  closeCoupon() {
    that.setData({ isget: !1, issucc: !1 })
  },
  toorder() {
    wx.redirectTo({
      url: '../orderList/orderList',
    })
  },
  return() {
    wx.switchTab({
      url: '../shop/shop',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    that.getdata()
    that.setData({
      iscon: !1,
      isshow: !1,
      wallets_password: '',
      ispay: !1,
      iserr: !1,
      isenough: !1,
      isnooilpay: !1,
      iswashpay: !1,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})